# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 8、结构化请求体构造JSON XML.py
@time : 2022/5/14 21:56
"""

# 主要是 json
import requests


class TestDemo:

    def test_post_json(self):
        payload = {
            "key1": "value1",
            "key2": "value2"
        }
        r = requests.post("https://httpbin.org/post", json=payload)
        """
        {
          "args": {}, 
          "data": "{\"key1\": \"value1\", \"key2\": \"value2\"}", 
          "files": {}, 
          "form": {}, 
          "headers": {
            "Accept": "*/*", 
            "Accept-Encoding": "gzip, deflate", 
            "Content-Length": "36", 
            "Content-Type": "application/json", 
            "Host": "httpbin.org", 
            "User-Agent": "python-requests/2.27.1", 
            "X-Amzn-Trace-Id": "Root=1-627fb6d1-7e73bece17943e6a283f505a"
          }, 
          "json": {
            "key1": "value1", 
            "key2": "value2"
          }, 
          "origin": "111.18.36.176", 
          "url": "https://httpbin.org/post"
        }

        """
        print(r.text)
        assert r.status_code == 200
        assert r.json()["json"]["key1"] == "value1"


"""
复杂数据解析：
数据保存： 将复杂的 xml 或者 json  请求体保存到文件模板中
数据处理：
    使用 mustache freemaker 等工具解析
    简单的字符串替换
    使用 json xml api 进行结构化解析
数据生成： 输出最终结果
"""

# xml 格式请求
import xmltodict
from requests import Response

xml = """<?xml version='1.0' encoding='utf-8'?>"""
headers = {'Content-Type': 'application/xml'}
r = requests.post('http://httpbin.org/post', data=xml, headers=headers).text
